package com.huawei.hianalytics.log.f;

import android.text.TextUtils;
import com.huawei.hianalytics.g.b;
import com.huawei.hianalytics.log.f.a.c;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

class d {
    static int a(List<c> list) {
        Closeable dataInputStream;
        Closeable closeable;
        Throwable th;
        Closeable closeable2 = null;
        if (list == null || list.size() == 0) {
            b.d("LogHttpClient", "uploadParameterList is empty!");
            return -1;
        }
        for (c cVar : list) {
            Object a = cVar.a();
            if (TextUtils.isEmpty(a)) {
                return -1;
            }
            URL url = new URL(a);
            String b = cVar.b();
            Map d = cVar.d();
            Closeable byteArrayOutputStream;
            try {
                File a2 = a(cVar.c());
                if (a2 == null) {
                    b.d("LogHttpClient", "not have file in bigzip! No access to the path,upload over");
                    com.huawei.hianalytics.log.e.d.a(7, null);
                    com.huawei.hianalytics.log.e.d.a(8, null);
                    return -1;
                }
                dataInputStream = new DataInputStream(new FileInputStream(a2));
                try {
                    byteArrayOutputStream = new ByteArrayOutputStream(WXMediaMessage.DESCRIPTION_LENGTH_LIMIT);
                    try {
                        byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
                        while (true) {
                            int read = dataInputStream.read(bArr);
                            if (read != -1) {
                                byteArrayOutputStream.write(bArr, 0, read);
                            } else {
                                int a3 = com.huawei.hianalytics.h.b.a(url.toString(), byteArrayOutputStream.toByteArray(), b, d).a();
                                com.huawei.hianalytics.log.e.d.a(7, dataInputStream);
                                com.huawei.hianalytics.log.e.d.a(8, byteArrayOutputStream);
                                return a3;
                            }
                        }
                    } catch (IOException e) {
                        closeable = byteArrayOutputStream;
                        byteArrayOutputStream = dataInputStream;
                    } catch (Throwable th2) {
                        th = th2;
                        closeable2 = byteArrayOutputStream;
                    }
                } catch (IOException e2) {
                    closeable = null;
                    byteArrayOutputStream = dataInputStream;
                    try {
                        b.d("LogHttpClient", "upLoadLogPutRequest() file input Stream Exception!");
                        com.huawei.hianalytics.log.e.d.a(7, byteArrayOutputStream);
                        com.huawei.hianalytics.log.e.d.a(8, closeable);
                    } catch (Throwable th3) {
                        closeable2 = closeable;
                        dataInputStream = byteArrayOutputStream;
                        th = th3;
                    }
                } catch (Throwable th4) {
                    th = th4;
                }
            } catch (IOException e3) {
                closeable = null;
                byteArrayOutputStream = null;
                b.d("LogHttpClient", "upLoadLogPutRequest() file input Stream Exception!");
                com.huawei.hianalytics.log.e.d.a(7, byteArrayOutputStream);
                com.huawei.hianalytics.log.e.d.a(8, closeable);
            } catch (Throwable th5) {
                th = th5;
                dataInputStream = null;
            }
        }
        return -1;
        com.huawei.hianalytics.log.e.d.a(7, dataInputStream);
        com.huawei.hianalytics.log.e.d.a(8, closeable2);
        throw th;
    }

    private static File a(String str) {
        File[] listFiles = new File(str).listFiles();
        return listFiles == null ? null : listFiles[0];
    }
}
